//
//  QianliAppearIndictor.swift
//  todoios
//
//  Created by 子凡 on 2025/7/31.
//

import Foundation
import UIKit

// 常量定义

class QianliAppearProgressIndicator: UIView {
    // 完成回调闭包
    
    private let zcHWxywRuZOjWF6w: CAShapeLayer
    private let To5PssOKMlFbaUwJ: CAShapeLayer
    private let AfITJbmNbOmasuRf: UILabel
    private var DreGDeCQ84e4nc1x: Bool = false
    private var YHblcFX8aHANLEvc: CGFloat = 0.0
    private var W8TH3KuIV0b0zU4g: CGFloat = 0.0
    
    private let E53zSOxD6FWbMYAB: TimeInterval = 0.3
    private let g7LARcbc9Ymv2gZl = "kTriangleOriginAnimationKey"
    private let aoX9Tv70PZ4T2Xtg = "kTriangleReadyAnimationKey"
    private let eEf4cO4vK7BXaDxQ = "kRectangleOriginAnimationKey"
    private let eAmTX4aQ4HK9bOX0 = "kRectangleReadyAnimationKey"
    
    
    var completeBlock: ((Bool) -> Void)?
    // 进度属性
    var progress: CGFloat = 0.0 {
        didSet {
            self.AfITJbmNbOmasuRf.text = String(format: "%d%%", Int(progress * 100))
            if progress == 1.0 {
                c0CWjuOYellsUaiS()
                self.DreGDeCQ84e4nc1x = true
            }
        }
    }
    
    // 初始化方法
    override init(frame: CGRect) {
        // 初始化图层
        self.zcHWxywRuZOjWF6w = QianliAppearProgressIndicator.DNfsqbWS4ApUmVLe()
        self.To5PssOKMlFbaUwJ = QianliAppearProgressIndicator.DNfsqbWS4ApUmVLe()
        self.AfITJbmNbOmasuRf = UILabel()
        
        super.init(frame: frame)
        z5FoekEhx4Vm1Joo()
    }
    
    required init?(coder: NSCoder) {
        // 初始化图层
        self.zcHWxywRuZOjWF6w = QianliAppearProgressIndicator.DNfsqbWS4ApUmVLe()
        self.To5PssOKMlFbaUwJ = QianliAppearProgressIndicator.DNfsqbWS4ApUmVLe()
        self.AfITJbmNbOmasuRf = UILabel()
        
        super.init(coder: coder)
        z5FoekEhx4Vm1Joo()
    }
    
    // 配置方法
    private func z5FoekEhx4Vm1Joo() {
        // 配置图层
        layer.addSublayer(self.zcHWxywRuZOjWF6w)
        layer.addSublayer(self.To5PssOKMlFbaUwJ)
        
        // 配置进度标签
        self.AfITJbmNbOmasuRf.frame = CGRect(x: 0, y: bounds.height/2 - 15, width: bounds.width, height: 15)
        self.AfITJbmNbOmasuRf.textAlignment = .center
        self.AfITJbmNbOmasuRf.font = UIFont.boldSystemFont(ofSize: 9)
        self.AfITJbmNbOmasuRf.text = "千里"
        self.AfITJbmNbOmasuRf.textColor = .white
        addSubview(self.AfITJbmNbOmasuRf)
        
        // 设置初始状态
        nI3PRYrIGS4Gpdd4()
    }
    
    // 创建默认图层
    private static func DNfsqbWS4ApUmVLe() -> CAShapeLayer {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.init(51, 102, 255).cgColor
        layer.strokeColor = UIColor.init(51, 102, 255).cgColor
        layer.lineCap = .round
        layer.lineJoin = .round
        return layer
    }
    
    // 初始状态
    private func nI3PRYrIGS4Gpdd4() {
        let width = bounds.width
        let height = bounds.height
        
        let triangleHeight = height / 4
        let triangleWidth = width / 2
        let rectangleWidth = triangleWidth / 2
        let rectangleHeight = triangleHeight
        
        // 矩形路径
        let rectangleRect = CGRect(
            x: (width - rectangleWidth) / 2,
            y: triangleHeight,
            width: rectangleWidth,
            height: rectangleHeight
        )
        let rectanglePath = UIBezierPath(roundedRect: rectangleRect, cornerRadius: 1)
        self.To5PssOKMlFbaUwJ.path = rectanglePath.cgPath
        self.To5PssOKMlFbaUwJ.frame = bounds
        
        // 三角形路径
        let startPoint = CGPoint(x: (width - triangleWidth) / 2, y: height / 2)
        let bottomPoint = CGPoint(x: width / 2, y: height - triangleHeight)
        let endPoint = CGPoint(x: startPoint.x + triangleWidth, y: startPoint.y)
        
        let trianglePath = UIBezierPath()
        trianglePath.move(to: startPoint)
        trianglePath.addLine(to: bottomPoint)
        trianglePath.addLine(to: endPoint)
        trianglePath.close()
        
        self.zcHWxywRuZOjWF6w.path = trianglePath.cgPath
        self.zcHWxywRuZOjWF6w.frame = bounds
    }
    
    // 准备下载动画
    func Fu54WsDms21twvGP() {
        self.AfITJbmNbOmasuRf.isHidden = false
        // 三角形动画
        let triAnimation = CABasicAnimation(keyPath: "transform")
        triAnimation.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.3, 0.3, 1))
        triAnimation.duration = self.E53zSOxD6FWbMYAB
        triAnimation.fillMode = .forwards
        triAnimation.isRemovedOnCompletion = false
        self.zcHWxywRuZOjWF6w.add(triAnimation, forKey: self.aoX9Tv70PZ4T2Xtg)
        
        // 矩形动画
        let recAnimation = CABasicAnimation(keyPath: "transform")
        recAnimation.toValue = NSValue(caTransform3D: CATransform3DMakeScale(1.5, 0.9, 1))
        recAnimation.duration = self.E53zSOxD6FWbMYAB
        recAnimation.fillMode = .forwards
        recAnimation.isRemovedOnCompletion = false
        self.To5PssOKMlFbaUwJ.add(recAnimation, forKey: self.eAmTX4aQ4HK9bOX0)
    }
    
    //成功动画
    private func c0CWjuOYellsUaiS() {
        completeBlock?(true)
        
        self.AfITJbmNbOmasuRf.text = "OK"
        self.AfITJbmNbOmasuRf.textColor = .green
        
        // 旋转动画
        let rotationAnim = CABasicAnimation(keyPath: "transform.rotation.y")
        rotationAnim.toValue = CGFloat.pi
        rotationAnim.duration = 0.25
        
        self.To5PssOKMlFbaUwJ.add(rotationAnim, forKey: nil)
        self.zcHWxywRuZOjWF6w.add(rotationAnim, forKey: nil)
        self.AfITJbmNbOmasuRf.layer.add(rotationAnim, forKey: nil)
    }
    
    // 下载失败动画
    func tm7V1xiSJMRpUa5c() {
        self.DreGDeCQ84e4nc1x = false
        
        self.AfITJbmNbOmasuRf.text = "failed"
        self.AfITJbmNbOmasuRf.textColor = .red
        
        // 旋转动画
        let rotationAnim = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnim.toValue = CGFloat.pi / 12  // M_PI_4/3
        rotationAnim.fillMode = .forwards
        rotationAnim.isRemovedOnCompletion = false
        rotationAnim.duration = 0.2
        layer.add(rotationAnim, forKey: "handleAppearLoadingFailedAnim")
    }
    
    // 布局变化时更新图层
    override func layoutSubviews() {
        super.layoutSubviews()
        self.zcHWxywRuZOjWF6w.frame = bounds
        self.To5PssOKMlFbaUwJ.frame = bounds
    }
}
    
